//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_recruitbuttonaction.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

if (__pGetVar(GVAR(recdbusy))) exitWith {};
__pSetVar [QGVAR(recdbusy), true];
private ["_exitj", "_rank", "_control", "_idx", "_torecruit", "_grp", "_unit", "_ctrl", "_pic", "_index", "_control2"];
if (GVAR(current_ai_num) >= GVAR(max_ai)) exitWith {
	format [(localize "STR_DOM_MISSIONSTRING_694"),GVAR(max_ai)] call FUNC(HQChat);
	__pSetVar [QGVAR(recdbusy), false];
};
__INC(GVAR(current_ai_num));
__TRACE_1("",GVAR(current_ai_num));

if (player != leader (group player)) exitWith {
	(localize "STR_DOM_MISSIONSTRING_695") call FUNC(HQChat);
	__pSetVar [QGVAR(recdbusy), false];
};

_exitj = false;
if (GVAR(with_ranked)) then {
	_rank = rank player;
	if (_rank == "PRIVATE") exitWith {
		(localize "STR_DOM_MISSIONSTRING_696") call FUNC(HQChat);
		_exitj = true;
	};

	if (score player < ((GVAR(points_needed) select 0) + (GVAR(ranked_a) select 3))) exitWith {
		(format [(localize "STR_DOM_MISSIONSTRING_697"), score player, GVAR(ranked_a) select 3]) call FUNC(HQChat);
		_exitj = true;
	};

	_max_rank_ai = switch (_rank) do {
		case "CORPORAL": {3};
		case "SERGEANT": {4};
		case "LIEUTENANT": {5};
		case "CAPTAIN": {6};
		case "MAJOR": {7};
		case "COLONEL": {8};
	};
	if (GVAR(current_ai_num) >= _max_rank_ai) exitWith {
		(format [(localize "STR_DOM_MISSIONSTRING_698"), _max_rank_ai]) call FUNC(HQChat);
		_exitj = true;
	};
	// each AI soldier costs score points
	[QGVAR(pas), [player, (GVAR(ranked_a) select 3) * -1]] call FUNC(NetCallEventCTS);
};

if (_exitj) exitWith {
	__pSetVar [QGVAR(recdbusy), false];
};

_control = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1000;
_idx = lbCurSel _control;
if (_idx == -1) exitWith {
	__pSetVar [QGVAR(recdbusy), false];
};

_torecruit = GVAR(UnitsToRecruit) select _idx;
_grp = group player;
_spawnpos = [];
if (player distance __XJIPGetVar(GVAR(AI_HUT)) < 20) then {
	_spawnpos = position GVAR(AISPAWN);
} else {
	if (!isNil QGVAR(additional_recruit_buildings)) then {
		{
			if (!isNil "_x" && {!isNull _x} && {player distance _x < 20}) exitWith {
				_spawnpos = player modelToWorld [0,-15,0];
			};
		} forEach GVAR(additional_recruit_buildings);
	};
};
if (count _spawnpos == 0) exitWith {
	__pSetVar [QGVAR(recdbusy), false];
};

_unit = _grp createUnit [_torecruit, _spawnpos, [], 0, "FORM"];
[_unit] join _grp;
_unit setSkill 1;
_unit setRank "PRIVATE";
if (GVAR(with_ranked)) then {
	_unit addEventHandler ["handleHeal", FUNC(HandleHeal)];
};
if (getNumber (configFile/"CfgVehicles"/typeOf _unit/"attendant") == 1) then {
	[_unit] execFSM "fsms\AIRevive.fsm";
};
[QGVAR(p_group), [_grp, player]] call FUNC(NetCallEventCTS);

GVAR(current_ai_units) set [count GVAR(current_ai_units), _unit];

_unit call FUNC(removefak);
_unit call FUNC(addInsignia);
_unit call FUNC(removenvgoggles);
_unit call FUNC(removetech);

if (GVAR(current_ai_num) == GVAR(max_ai)) then {
	(__uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1010) ctrlShow false;
};

_ctrl = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1011;
if (!ctrlShown _ctrl) then {
	_ctrl ctrlShow true;
};
_ctrl = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1012;
if (!ctrlShown _ctrl) then {
	_ctrl ctrlShow true;
};

_control = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1001;
_pic = getText (configFile/"cfgVehicles"/_torecruit/"picture");
_index = _control lbAdd ([_torecruit,0] call FUNC(GetDisplayName));
_control lbSetPicture [_index, _pic];
_control lbSetColor [_index, [1, 1, 0, 0.8]];

_control2 = __uiGetVar(GVAR(RECRUIT_DIALOG)) displayCtrl 1030;
_control2 ctrlSetText format [(localize "STR_DOM_MISSIONSTRING_693"), GVAR(current_ai_num), GVAR(max_ai)];

__pSetVar [QGVAR(recdbusy), false];